//
//  SEGMarketViewModel.m
//  Commom
//
//  Created by Clavis on 2019/7/29.
//

#import "SEGMarketViewModel.h"
#import <Commom/SEGColorManager.h>
#import <Commom/SEGLayoutMacro.h>

@implementation SEGMarketViewModel
-(instancetype)init{
    if (self == [super init]) {
        _mainTitle = @"";
        _contentTitle = @"";
        _price = @"";
        _image = @"";
        _widgetWidth = LYScreenWidth();
        
    }
    return self;
}
@end

/**************************SEGMarketViewLayout************************/
@implementation SEGMarketViewLayout
- (instancetype)initWithData:(SEGMarketViewModel *)data{
    if (self == [super init]) {
        _data = data;
    }
    [self segMarketViewLayout];
    return self;
}

-(void)segMarketViewLayout{
    //高度固定
    //    _totalHeight = kMarketViewHeight;
    
    //高度不固定
    _totalHeight = 0;
    _mainTitleLabelHeight = 0;
    _mainTitleLabelTextLayout = nil;
    _contentTitleLabelHeight = 0;
    _contentTitleLabelTextLayout = nil;
    _priceLabelHeight = 0;
    _priceLabelTextLayout = nil;
    
    if (!_data) return;
    
    [self layout];
    _totalHeight += 10;
    _totalHeight += _mainTitleLabelHeight;
    _totalHeight += 100;
}

//数据处理
-(void)layout{
    [self segpriceLabelWithContent:_data.price fontSize:15 textColor:kColor_S1];
    [self segmainTitleLabelWithContent:_data.mainTitle fontSize:15 textColor:kColor_1];
    [self segcontentTitleLabelWithContent:_data.contentTitle fontSize:13 textColor:kColor_1];
}
//控件contentTitleLabel文本布局计算
-(void)segmainTitleLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont systemFontOfSize:fontSize];//[UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(_data.widgetWidth - _priceLabelWidth - 20, HUGE);
    container.linePositionModifier = modifier;
    container.maximumNumberOfRows = 1;
    container.truncationType = SEGTextTruncationTypeEnd;
    
    _mainTitleLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_mainTitleLabelTextLayout)return;
    _mainTitleLabelHeight = [modifier heightForLineCount:_mainTitleLabelTextLayout.rowCount];
}

//控件contentTitleLabel文本布局计算
-(void)segcontentTitleLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont systemFontOfSize:fontSize];//[UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(_data.widgetWidth - 105, HUGE);
    container.linePositionModifier = modifier;
    container.maximumNumberOfRows = 4;
    container.truncationType = SEGTextTruncationTypeEnd;
    
    _contentTitleLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_contentTitleLabelTextLayout)return;
    _contentTitleLabelHeight = [modifier heightForLineCount:_contentTitleLabelTextLayout.rowCount];
}

//控件priceLabel文本布局计算
-(void)segpriceLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont systemFontOfSize:fontSize];//[UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(LYScreenWidth(), HUGE);
    container.linePositionModifier = modifier;
    
    _priceLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_priceLabelTextLayout)return;
    _priceLabelHeight = [modifier heightForLineCount:_priceLabelTextLayout.rowCount];
    
    CGFloat w = [SEGKitHelper seg_sizeWithString:content fontSize:fontSize textColor:color].width;
    _priceLabelWidth = w;
}

@end
